﻿using System;
using System.Collections.Generic;
using System.Linq;
using SGQ.Model;
using System.Data.SqlServerCe;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Windows.Forms;

namespace SGQ.Controller
{
    class ControllerLogin
    {
        public ControllerLogin() { 
            conn = new ConnectFactory();
        }

        private ConnectFactory conn; 
        public const string strSelect = "Select * From Usuario Where Senha = (@Senha) and Login = (@Login) order by Codigo";
        private ConnectFactory objConexao;

      
		public Usuario ListDataSet(Usuario user) 
        {
            try
            {
                using (objConexao = new ConnectFactory())
                {
                    using (SqlCeCommand objCommand = new SqlCeCommand(strSelect, objConexao.CreateConnection()))
                    {
                        objCommand.Parameters.AddWithValue("@Senha", user.Login.Senha);
                        objCommand.Parameters.AddWithValue("@Login", user.Login.Name);
                        SqlCeDataAdapter adapter = new SqlCeDataAdapter(objCommand);
						DataSet dataSet = new DataSet();
						adapter.Fill(dataSet);

                        user.Codigo = int.Parse(dataSet.Tables[0].Rows[0].ItemArray[0].ToString());
                        user.Departamento.Codigo = int.Parse(dataSet.Tables[0].Rows[0].ItemArray[5].ToString());
                        user.Nome = dataSet.Tables[0].Rows[0].ItemArray[1].ToString();
                        user.Login.Name = dataSet.Tables[0].Rows[0].ItemArray[2].ToString();
                        user.Login.Senha = dataSet.Tables[0].Rows[0].ItemArray[3].ToString();
                        if ("False" == dataSet.Tables[0].Rows[0].ItemArray[4].ToString())
                        {
                            user.Login.Tipo = 0;
                        }
                        else
                        {
                            user.Login.Tipo = 1;
                        }
                        return (user);
                    }
                }
            }catch (Exception e)
            {
                throw new Exception(e.Message,e);
            }
        }

    }
}
